package person.tanjinfu.autorepository.blo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import person.tanjinfu.autorepository.entity.Product;
import person.tanjinfu.autorepository.repository.IProductRepository;

import java.util.List;


@Component
@Slf4j
public class ProductBlo {

    @Autowired
    private IProductRepository iProductRepository;


    public Product create(Product product) {
        log.info("Create product...");
        Product createdProduct = this.iProductRepository.insert(product);
        log.info("Create product [success].");
        return createdProduct;
    }

    public List<Product> listByCategoryId(String categoryId) {
        log.info("List by category id... categoryId:{}.", categoryId);
        List<Product> productList = this.iProductRepository.findByCategoryId(categoryId);
        log.info("List by category id [success]. categoryId:{}, itemsCount:{}.", categoryId, productList.size());
        return productList;
    }

    public void removeByCategoryId(String categoryId) {
        log.info("Remove by categoryId... categoryId:{}.", categoryId);
        this.iProductRepository.deleteByCategoryId(categoryId);
        log.info("Remove by categoryId [success]. categoryId:{}.", categoryId);
    }


}
